<!--

    Copyright (C) 2015 The Gravitee team (http://gravitee.io)

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

            http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.

-->
<div class="subscriptions__header">
  <div class="subscriptions__header__title">
    <h2>Manage your subscriptions</h2>
  </div>

  <div *ngIf="subscriptionsTableDS && !isLoadingData" class="subscriptions__header__buttons">
    <button
      *gioPermission="{ anyOf: ['api-subscription-r'] }"
      mat-raised-button
      type="button"
      [disabled]="isLoadingData || subscriptionsTableDS.length === 0"
      aria-label="Export subscriptions list"
      (click)="exportAsCSV()"
    >
      <mat-icon svgIcon="gio:upload"></mat-icon>Export as CSV
    </button>
    <button
      *gioPermission="{ anyOf: ['api-subscription-c'] }"
      mat-raised-button
      type="button"
      color="primary"
      aria-label="Create a subscription"
      (click)="createSubscription()"
      [disabled]="api.definitionVersion === 'V1'"
    >
      <mat-icon svgIcon="gio:plus"></mat-icon>Create a subscription
    </button>
  </div>
</div>

<mat-card class="subscriptions__filters" [formGroup]="filtersForm">
  <mat-card-content>
    <div class="subscriptions__filters__inputs">
      <mat-form-field class="subscriptions__filters__inputs__field">
        <mat-label>Plan</mat-label>
        <mat-select formControlName="planIds" [multiple]="true">
          <mat-option *ngFor="let plan of plans" [value]="plan.id">{{ plan.name }}</mat-option>
        </mat-select>
      </mat-form-field>

      <mat-form-field class="subscriptions__filters__inputs__field">
        <mat-label>Application</mat-label>
        <gio-form-tags-input
          [placeholder]="'Search for an application'"
          formControlName="applicationIds"
          [autocompleteOptions]="searchApplications"
          [displayValueWith]="displayApplication"
          [useAutocompleteOptionValueOnly]="true"
        ></gio-form-tags-input>
      </mat-form-field>

      <mat-form-field class="subscriptions__filters__inputs__field">
        <mat-label>Status</mat-label>
        <mat-select formControlName="statuses" [multiple]="true">
          <mat-option *ngFor="let status of statuses" [value]="status.id">{{ status.name }}</mat-option>
        </mat-select>
      </mat-form-field>

      <mat-form-field class="subscriptions__filters__inputs__field">
        <mat-label>API Key</mat-label>
        <input matInput formControlName="apiKey" />
      </mat-form-field>
    </div>
    <div class="subscriptions__filters__buttons">
      <button mat-raised-button type="button" color="secondary" aria-label="Reset filters" (click)="resetFilters()">
        <mat-icon svgIcon="gio:refresh-cw"></mat-icon>Reset filters
      </button>
    </div>
  </mat-card-content>

  <gio-table-wrapper
    [disableSearchInput]="true"
    [length]="nbTotalSubscriptions"
    [filters]="filtersStream.value.tableWrapper"
    (filtersChange)="onFiltersChanged($event)"
  >
    <table
      mat-table
      [dataSource]="subscriptionsTableDS"
      class="subscriptions__table"
      id="subscriptionsTable"
      aria-label="Subscriptions table"
    >
      <!-- Display Security Type Column -->
      <ng-container matColumnDef="securityType">
        <th mat-header-cell *matHeaderCellDef id="securityType">Security type</th>
        <td mat-cell *matCellDef="let element">
          {{ element.securityType }}
          @if (element.isSharedApiKey) {
            <span class="gio-badge-accent" matTooltip="Use shared API keys">Shared</span>
          }
        </td>
      </ng-container>

      <!-- Display Plan Column -->
      <ng-container matColumnDef="plan">
        <th mat-header-cell *matHeaderCellDef id="name">Plan</th>
        <td mat-cell *matCellDef="let element">
          {{ element.plan }}
        </td>
      </ng-container>

      <!-- Display Application Column -->
      <ng-container matColumnDef="application">
        <th mat-header-cell *matHeaderCellDef id="application">Application</th>
        <td mat-cell *matCellDef="let element">
          {{ element.application }}
        </td>
      </ng-container>

      <!-- Display Created at Column -->
      <ng-container matColumnDef="createdAt">
        <th mat-header-cell *matHeaderCellDef id="createdAt">Created at</th>
        <td mat-cell *matCellDef="let element">
          {{ element.createdAt | date: 'yyyy-MM-dd HH:mm:ss' }}
        </td>
      </ng-container>

      <!-- Display Processed at Column -->
      <ng-container matColumnDef="processedAt">
        <th mat-header-cell *matHeaderCellDef id="processedAt">Processed at</th>
        <td mat-cell *matCellDef="let element">
          {{ element.processedAt | date: 'yyyy-MM-dd HH:mm:ss' }}
        </td>
      </ng-container>

      <!-- Display Start at Column -->
      <ng-container matColumnDef="startingAt">
        <th mat-header-cell *matHeaderCellDef id="startingAt">Start at</th>
        <td mat-cell *matCellDef="let element">
          {{ element.startingAt | date: 'yyyy-MM-dd HH:mm:ss' }}
        </td>
      </ng-container>

      <!-- Display End at Column -->
      <ng-container matColumnDef="endAt">
        <th mat-header-cell *matHeaderCellDef id="endAt">End at</th>
        <td mat-cell *matCellDef="let element">
          {{ element.endAt | date: 'yyyy-MM-dd HH:mm:ss' }}
        </td>
      </ng-container>

      <!-- Display Status Column -->
      <ng-container matColumnDef="status">
        <th mat-header-cell *matHeaderCellDef id="status">Status</th>
        <td mat-cell *matCellDef="let element">
          <span [class]="'gio-badge-' + element.statusBadge">{{ element.status }}</span>
        </td>
      </ng-container>

      <!-- TODO? add analytics preview Column -->

      <!-- Actions Column -->
      <ng-container matColumnDef="actions">
        <th mat-header-cell *matHeaderCellDef id="actions"></th>
        <td mat-cell *matCellDef="let element">
          <div class="subscriptions__table__actions">
            <ng-container *ngIf="canUpdate; else readOnlyPlanActions">
              <button
                mat-icon-button
                aria-label="Edit the subscription"
                matTooltip="Edit the subscription"
                [routerLink]="'./' + element.id"
              >
                <mat-icon svgIcon="gio:edit-pencil"></mat-icon>
              </button>
            </ng-container>
            <ng-template #readOnlyPlanActions>
              <button
                *gioPermission="{ anyOf: ['api-subscription-r'] }"
                mat-icon-button
                aria-label="View the subscription details"
                matTooltip="View the subscription details"
                [routerLink]="'./' + element.id"
              >
                <mat-icon svgIcon="gio:eye-empty"></mat-icon>
              </button>
            </ng-template>
          </div>
        </td>
      </ng-container>

      <tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>
      <tr mat-row *matRowDef="let row; columns: displayedColumns"></tr>

      <!-- Row shown when there is no data -->
      <tr class="mat-mdc-row mdc-data-table__row" *matNoDataRow>
        <td
          *ngIf="!isLoadingData && subscriptionsTableDS.length === 0"
          class="mat-mdc-cell mdc-data-table__cell"
          [attr.colspan]="displayedColumns.length"
        >
          {{ 'There is no subscription (yet).' }}
        </td>
        <td *ngIf="isLoadingData" class="mat-mdc-cell mdc-data-table__cell loader" [attr.colspan]="displayedColumns.length">
          <gio-loader></gio-loader>
        </td>
      </tr>
    </table>
  </gio-table-wrapper>
</mat-card>
